﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace SPTotalAutomationToolkit.SP2013IntranetProjectTemplate.Wizard
{
    public partial class Page2b : UserControl
    {
        private WizardWindow mainWindow;

        internal Page2b(WizardWindow mainWindow)
        {
            this.mainWindow = mainWindow;
            InitializeComponent();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            //hnscServerNameForDevTextBox.Text = mainWindow.PresentationModel.ServerName;
        }

        private void hnscIntranetHostHeaderForDevTextBox_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Space)
            {
                e.Handled = true;
            }
            base.OnPreviewKeyDown(e);
        }

        private void hnscIntranetHostHeaderForDevTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            // Remove any spaces that may have been pasted in
            hnscIntranetHostHeaderForDevTextBox.Text = hnscIntranetHostHeaderForDevTextBox.Text.Replace(" ", "");

            var hostHeader = hnscIntranetHostHeaderForDevTextBox.Text;
            var url = EnsureTrailingSlash("http://" + hostHeader + ":" + hnscServerPortForDevTextBox.Text);

            // Update the label on the wizard UI to display the url
            hnscIntranetUrlForDevLabel.Content = url;

            mainWindow.PresentationModel.IntranetWebAppHostHeaderForDev = hostHeader;
            mainWindow.PresentationModel.SiteUrlForDebugging = url;

            ValidateForm();
        }

        private void hnscPeopleHostHeaderForDevTextBox_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Space)
            {
                e.Handled = true;
            }
            base.OnPreviewKeyDown(e);
        }

        private void hnscPeopleHostHeaderForDevTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            // Remove any spaces that may have been pasted in
            hnscPeopleHostHeaderForDevTextBox.Text = hnscPeopleHostHeaderForDevTextBox.Text.Replace(" ", "");

            var hostHeader = hnscPeopleHostHeaderForDevTextBox.Text;
            var url = EnsureTrailingSlash("http://" + hostHeader + ":" + hnscPeoplePortForDevTextBox.Text);

            // Update the label on the wizard UI to display the url
            hnscPeopleUrlForDevLabel.Content = url;

            mainWindow.PresentationModel.PeopleWebAppHostHeaderForDev = hostHeader;

            ValidateForm();
        }

        private void hnscPeoplePortForDevTextBox_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Space)
            {
                e.Handled = true;
            }
            base.OnPreviewKeyDown(e);
        }

        private void hnscPeoplePortForDevTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            // Remove any spaces that may have been pasted in
            hnscPeoplePortForDevTextBox.Text = hnscPeoplePortForDevTextBox.Text.Replace(" ", "");

            var port = hnscPeoplePortForDevTextBox.Text;
            var url = EnsureTrailingSlash("http://" + hnscPeopleHostHeaderForDevTextBox.Text + ":" + port);

            // Update the label on the wizard UI to display the url
            hnscPeopleUrlForDevLabel.Content = url;

            mainWindow.PresentationModel.PeopleWebAppPortForDev = port;

            ValidateForm();
        }

        private void hnscServerNameForDevTextBox_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Space)
            {
                e.Handled = true;
            }
            base.OnPreviewKeyDown(e);
        }

        private void hnscServerNameForDevTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            // Remove any spaces that may have been pasted in
            hnscServerNameForDevTextBox.Text = hnscServerNameForDevTextBox.Text.Replace(" ", "");

            var hostHeader = hnscServerNameForDevTextBox.Text;
            var url = EnsureTrailingSlash("http://" + hostHeader + ":" + hnscServerPortForDevTextBox.Text);

            // Update the label on the wizard UI to display the url
            hnscServerUrlForDevLabel.Content = url;

            mainWindow.PresentationModel.ServerNameForDevWithFQDN = hostHeader;

            ValidateForm();
        }

        private void hnscServerPortForDevTextBox_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Space)
            {
                e.Handled = true;
            }
            base.OnPreviewKeyDown(e);
        }

        private void hnscServerPortForDevTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {

            // Remove any spaces that may have been pasted in
            hnscServerPortForDevTextBox.Text = hnscServerPortForDevTextBox.Text.Replace(" ", "");

            var port = hnscServerPortForDevTextBox.Text;
            var url = EnsureTrailingSlash("http://" + hnscServerNameForDevTextBox.Text + ":" + port);
            
            // Update the label on the wizard UI to display the url
            hnscServerUrlForDevLabel.Content = url;

            mainWindow.PresentationModel.ServerPortForDev = port;

            // Update the intranet port

            var intranetUrl = EnsureTrailingSlash("http://" + hnscIntranetHostHeaderForDevTextBox.Text + ":" + port);

            // Update the label on the wizard UI to display the url
            hnscIntranetUrlForDevLabel.Content = intranetUrl;

            mainWindow.PresentationModel.IntranetWebAppPortForDev = port;

            ValidateForm();
        }

        private string EnsureTrailingSlash(string url)
        {
            if (!String.IsNullOrEmpty(url)
                && url[url.Length - 1] != '/')
            {
                url += '/';
            }
            return url;
        }

        private void WriteIntranetHostFile_Checked(object sender, RoutedEventArgs e)
        {
            mainWindow.PresentationModel.WriteIntranetHostFileEntry = (bool)writeIntranetHostCheckbox.IsChecked;
        }

        private void WriteIntranetHostFile_UnChecked(object sender, RoutedEventArgs e)
        {
            mainWindow.PresentationModel.WriteIntranetHostFileEntry = (bool)writeIntranetHostCheckbox.IsChecked;
        }

        private void WritePeopleHostFile_Checked(object sender, RoutedEventArgs e)
        {
            mainWindow.PresentationModel.WritePeopleHostFileEntry = (bool)writePeopleHostCheckbox.IsChecked;
        }

        private void WritePeopleHostFile_UnChecked(object sender, RoutedEventArgs e)
        {
            mainWindow.PresentationModel.WritePeopleHostFileEntry = (bool)writePeopleHostCheckbox.IsChecked;
        }
        
        private void ValidateForm()
        {
            var formIsValid = true;

            if (hnscIntranetHostHeaderForDevTextBox.Text == "")
            {
                formIsValid = false;
            }

            if (hnscPeopleHostHeaderForDevTextBox.Text == "")
            {
                formIsValid = false;
            }

            if (hnscPeoplePortForDevTextBox.Text == "")
            {
                formIsValid = false;
            }

            if (hnscServerNameForDevTextBox.Text == "")
            {
                formIsValid = false;
            }

            if (hnscServerPortForDevTextBox.Text == "")
            {
                formIsValid = false;
            }

            if (formIsValid)
            {
                mainWindow.nextButton.IsEnabled = true;
            }
            else
            {
                mainWindow.nextButton.IsEnabled = false;
            }
        }

    }
}